// threadtest.cc 
//	Simple test case for the threads assignment.
//
//	Create two threads, and have them context switch
//	back and forth between themselves by calling Thread::Yield, 
//	to illustratethe inner workings of the thread system.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#include "copyright.h"
#include "system.h"
//----------------------------------------------------------------------
// SimpleThread
// 	Loop 5 times, yielding the CPU to another ready thread 
//	each iteration.
//
//	"which" is simply a number identifying the thread, for debugging
//	purposes.
//----------------------------------------------------------------------
int L = 0;
//Mailbox * box = NULL;
int inputCheck()
{
 char c [100];
 char d [100];
 fgets(c, 100, stdin);
 //check for improper input.
 if( !sscanf(c, "%[^1234567890]", d))
 {
	int u = atoi(c);
	printf("int %d \n", u);
	return u;
 }
 else
 {
	printf("Error: input must be int that is not 0 \n");
	return 0;
 }
}
void mailSim(int which)
{
	do
	{
		//read all messages
		//box[which].read;
		L--;
	}while(L > 0);
	//read all messages in person's own mailbox
	/*
	
	*/   
}
void
SimpleThread(int which)
{
    int num;
    
    for (num = 0; num < 5; num++) {
	printf("*** thread %d looped %d times\n", which, num);
        currentThread->Yield();
    }
}


//----------------------------------------------------------------------
// ThreadTest
// 	Invoke a test routine.
//----------------------------------------------------------------------

void
ThreadTest()
{
    DEBUG('t', "Entering ThreadTest");
    //This series of if, elseif statements determines which option
    //the user selected.
   /*	if(!strcmp(AFlag, "0"))
	{ 
	//Thread *t = new Thread("forked thread");
	//t->Fork(SimpleThread, 1);
	//SimpleThread(0);
	}
	
	else if(!strcmp(AFlag,"1"))
	{
	 printf("Shout assignment \n");
	}

	else if(!strcmp(AFlag,"2"))
	{
	 printf("Philosophers with semaphores \n");
	}

	else if(!strcmp(AFlag,"3"))
	{
	 printf("Philosophers with busy while loops \n");
	}

	else if(!strcmp(AFlag,"4"))
	{
	 printf("Mailbox simulator \n");
	 //prompt the user for input
	 //char c [100];
	 //char d [100];
	 int X = 0;
	 int N = 0;
	 printf("Input integer for number of people \n");	 
	 X = inputCheck();
	 if(X == 0)
	 {

	 }
	 else
	 {
	  printf("Input integer for size of mailbox \n");
	  N = inputCheck();
	  if(N == 0)
	  {

	  }
	  else
	  {
	   printf("Input number of sends til end of sim \n");
	   L = inputCheck();
	   if(L == 0)
	   {
	   }
	   else
	   {
	    printf("%d %d %d \n", X, N, L);
	    Thread * people [X];
	    for(int i = 0; i < X ; i++)
	    {
	     people[i] = new Thread("person");
	    }
	 */
	 /* box = new Mailbox[X];
	    for(int i = 0; i < X; i++)
	    {
		box[i].setMailbox(i, N);
	    }
	*//*	    
	   for(int i = 0; i < X ; i++)
	   {
	   	people[i]->Fork(mailSim, i);
	   }
	   mailSim(0);
	
	 //Thread *t = new Thread("forked thread");
	 //t->Fork(SimpleThread, 1);
	 //SimpleThread(0);
	   }}}
	}
	else if(!strcmp(AFlag, "5"))
	{
	 printf("mailbox sim with sem on every box \n");
	}
	else
	{
	 printf("  -- end error \n");
	}*/
}

/*
Mailbox class {
Public:
	Mailbox();
	read();
	readSem();
	send(int recipient, Message object);
	semSend(int recipient, Message object);
	isFull();

	Private:
	struct message
	{
	 int sender;
	 char * message;
	}
	//semaphore
	Message inbox[N];
}*/
